#encoding=utf8

import httplib
import urllib
import urllib2
import cookielib
import time
import random
import datetime
import threading

class LogWriter():
    f=''
    def __init__(self):
        self.f=open('log.txt','w')
    def write(self,str_data):
        self.f.write(str_data)
        self.f.write('\n')
        self.f.flush()
class User():
    pwuser=''
    pwpwd=''
    jumpurl=''
    lgt=''
    step=''

    def __init__(self,pwuser, pwpwd, jumpurl='http://bbs.saraba1st.com/2b/index.php?m=bbs', lgt='0', step='2'):
        self.pwuser=pwuser
        self.pwpwd=pwpwd
        self.jumpurl=jumpurl
        self.lgt=lgt
        self.step=step
    def get_params(self):
        return urllib.urlencode({'pwuser':self.pwuser,'pwpwd':self.pwpwd,'jumpurl':self.jumpurl,'lgt':self.lgt,'step':self.step})

class S1(object):
    def __init__(self):
        self.cj=cookielib.LWPCookieJar()
        self.handler=''
        self.user=''
        self.log_writer=LogWriter()
        try:
            self.cj.revert('s1.cookie')
        except Exception, e:
            #print e
            self.log_writer.write(str(e))

        self.opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
        urllib2.install_opener(self.opener)

    def login(self,user):
        self.user=user
        #print 'login '+user.pwuser
        self.log_writer.write('login '+user.pwuser)
        req=urllib2.Request('http://bbs.saraba1st.com/2b/login.php',user.get_params())
        req=self.complete_header(req)
        self.handler=self.opener.open(req)
        self.cj.save('s1.cookie')
        #self.viewinfo()
        #print self.user.pwuser+'login success. Refreshing action.'
        self.log_writer.write(self.user.pwuser+'login success. Refreshing action.')
        self.refresher()

    def complete_header(self,req):
        req.add_header('Host','bbs.saraba1st.com')
        req.add_header('User-Agent','Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.19) Gecko/20110707 Firefox/7.0.1')
        return req

    def refresher(self):
        while(1):
            req_top=urllib2.Request('http://bbs.saraba1st.com/2b/')
            req_top=self.complete_header(req_top)
            self.handler=self.opener.open(req_top)
            #print str(datetime.datetime.now())+' '+self.user.pwuser+' refreshed.'
            self.log_writer.write(str(datetime.datetime.now())+' '+self.user.pwuser+' refreshed.')
            sleep_time=random.randint(600,1800)
            time.sleep(sleep_time)

class UserThread(threading.Thread):
    def __init__(self,user):
        threading.Thread.__init__(self)
        self.user=user

    def run(self):
        s1=S1()
        s1.login(self.user)

users=[User(pwuser='user1', pwpwd='pw1'),\
        User(pwuser='user2', pwpwd='pw2`'),\
        User(pwuser='user3', pwpwd='pw3')]
#conn=init_conn()
#login(user1,conn)
#getTopPage(conn)
#s1=S1()
#s1.login(user1)
for user in users:
    t=UserThread(user)
    t.start()
